package com.yanqu.road.server.manager.activity;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manager.chefduel.CrossChefDuelActivityMgr;
import com.yanqu.road.server.manager.cookboy.CrossCookBoyActivityMgr;
import com.yanqu.road.utils.date.DateHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;


public class CrossRelationActivityMgr extends TempMgr {

    protected static Logger logger = LogManager.getLogger(CrossRelationActivityMgr.class.getName());

    //触发条件定义
    public static int EVENT_RANK_USER_TYPE = 1;//个人通用排行榜触发条件
    public static int EVENT_RANK_UNION_TYPE = 2;//商会通用排行榜触发条件
    public static int EVENT_RANK_SERVER_TYPE = 3;//区服通用排行榜触发条件

    //activityId,playerEventType,RelationActivityMgrEvent,playerEventType,触发事件可能会一样，加上活动
    protected static Map<Integer,Map<Integer,Integer>> activityConditionTypeMap = new ConcurrentHashMap<>();

    protected static Map<Integer,ActivityInfo> activityInfoMap = new ConcurrentHashMap<>();

    //activityId
    protected static Map<Integer, List<ActivityConditionInfo>> activityConditionInfoMap = new ConcurrentHashMap<>();

    //activityId type
    protected static Map<Integer, Map<Integer, List<ActivityConditionInfo>>> activityConditionInfoTypeMap = new ConcurrentHashMap<>();


    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean save() {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        loadActivity();
        return true;
    }

    public static int getEventConditionType(int activityId,int eventType){
        if(activityConditionTypeMap.containsKey(activityId)){
            return activityConditionTypeMap.get(activityId).getOrDefault(eventType,0);
        }
        return 0;
    }

    public static void addConditionType(int activityId,Map<Integer,Integer> conditionTypeMap){
        activityConditionTypeMap.put(activityId,conditionTypeMap);
    }

    public static void loadActivity(){
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.CookingBoyActivity.getValue());
        activityTypeList.add(eActivityType.ChefDuel.getValue());
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        Map<Integer, ActivityInfo> tempActivityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList,nowTimeStr, CrossBaseServer.getInstance().getServerId());
        List<Integer> activityIdList = new ArrayList<>();
        for (ActivityInfo activityInfo : tempActivityInfoMap.values()) {
            activityIdList.add(activityInfo.getActivityId());
        }

        Map<Integer, List<ActivityConditionInfo>> tempActivityConditionInfoMap = new ConcurrentHashMap<>();
        Map<Integer, Map<Integer, List<ActivityConditionInfo>>> tempActivityConditionTypeInfoMap = new ConcurrentHashMap<>();
        ActivityBussiness.getActivityConditionInfoMap(tempActivityConditionTypeInfoMap, tempActivityConditionInfoMap, activityIdList);
        activityInfoMap = tempActivityInfoMap;
        activityConditionInfoMap = tempActivityConditionInfoMap;
        activityConditionInfoTypeMap = tempActivityConditionTypeInfoMap;

        CrossCookBoyActivityMgr.loadChildActivity(activityInfoMap);
        CrossChefDuelActivityMgr.loadChildActivity(activityInfoMap);
    }



    public static void loadActivity(ActivityInfo activityInfo){
        List<ActivityInfo> infoList = new ArrayList<>();
        infoList.add(activityInfo);
        loadActivity(infoList);
    }

    public static void loadActivity(List<ActivityInfo> activityInfoList){
        Map<Integer,ActivityInfo> activityMap = new ConcurrentHashMap<>();
        List<Integer> activityIdList = new ArrayList<>();
        for(ActivityInfo activityInfo : activityInfoList){
            activityInfoMap.put(activityInfo.getActivityId(),activityInfo);
            activityIdList.add(activityInfo.getActivityId());
            activityMap.put(activityInfo.getActivityId(),activityInfo);
        }
        Map<Integer, List<ActivityConditionInfo>> tempActivityConditionInfoMap = new ConcurrentHashMap<>();
        Map<Integer, Map<Integer, List<ActivityConditionInfo>>> tempActivityConditionTypeInfoMap = new ConcurrentHashMap<>();
        ActivityBussiness.getActivityConditionInfoMap(tempActivityConditionTypeInfoMap, tempActivityConditionInfoMap, activityIdList);
        for(int activityId : tempActivityConditionInfoMap.keySet()){
            activityConditionInfoMap.put(activityId,tempActivityConditionInfoMap.get(activityId));
        }
        for(int activityId : tempActivityConditionTypeInfoMap.keySet()){
            activityConditionInfoTypeMap.put(activityId,tempActivityConditionTypeInfoMap.get(activityId));
        }

        CrossCookBoyActivityMgr.loadChildActivity(activityMap);
        CrossChefDuelActivityMgr.loadChildActivity(activityMap);
    }

    /**
     * 刷新活动配置
     * @param activityId    活动ID
     * @return
     */
    public static ActivityInfo reloadActivityInfo(int activityId) {
        if(activityInfoMap.containsKey(activityId)){
            return activityInfoMap.get(activityId);
        }
        ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
        logger.info("跨服成功加载厨神系列活动,活动id{},活动大类型{},活动小类型{}",activityInfo.getActivityId(),activityInfo.getType(),activityInfo.getChildType());
        if (activityInShowTime(activityInfo)) {
            activityInfoMap.put(activityId, activityInfo);
            loadActivity(activityInfo);
            return activityInfo;
        }
        return null;
    }

    public static AtomicBoolean reloadActivityInfoStat = new AtomicBoolean(false);

    public static void reloadActivityInfo() {

        try {
            boolean oldValue = reloadActivityInfoStat.getAndSet(true);
            if (oldValue) {
                return;
            }
            logger.debug("重新加载 reloadActivityInfo start");
            for(ActivityInfo activityInfo : activityInfoMap.values()){
                if (activityInShowTime(activityInfo)) {
                    return;//有活动在时间内，不需要重新加载,开一半要过期或者改时间,改配置,手动重载这个类
                }
                ActivityInfo newActivityInfo = ActivityBussiness.getActivityInfoByActivityId(activityInfo.getActivityId());
                if (activityInShowTime(newActivityInfo)) {
                    activityInfoMap.put(newActivityInfo.getActivityId(), newActivityInfo);
                }
            }
            CrossRelationActivityMgr.loadActivity(new ArrayList<>(activityInfoMap.values()));
        } catch (Exception ex) {
            logger.error("重新加载 reloadActivityInfo error");
        } finally {
            reloadActivityInfoStat.set(false);
        }

    }

    public static ActivityInfo getOpenActivityInfo(int activityId){
        if(!activityInfoMap.containsKey(activityId)){
            return null;
        }
        ActivityInfo activityInfo = activityInfoMap.get(activityId);
        if(activityInShowTime(activityInfo) && activityInfo.getActivityId() == activityId){
            return activityInfo;
        }
        return null;
    }

    public static List<ActivityInfo> getOpenActivityInfoList() {
        List<ActivityInfo> dataList = new ArrayList<>();
        for(ActivityInfo activityInfo : activityInfoMap.values()) {
            if (activityInShowTime(activityInfo)) {
                dataList.add(activityInfo);
            }
        }
        return dataList;
    }

    public static List<ActivityInfo> getOpenActivityInfoList(int type, int childType) {
        List<ActivityInfo> dataList = new ArrayList<>();
        for(ActivityInfo activityInfo : activityInfoMap.values()) {
            if (activityInShowTime(activityInfo) && activityInfo.getChildType() == childType && activityInfo.getType() == type) {
                dataList.add(activityInfo);
            }
        }
        return dataList;
    }

    public static Map<Integer,ActivityInfo> getOpenActivityInfoMap() {
        Map<Integer,ActivityInfo> dataMap = new ConcurrentHashMap<>();
        for(ActivityInfo activityInfo : activityInfoMap.values()) {
            if (activityInShowTime(activityInfo)) {
                dataMap.put(activityInfo.getActivityId(),activityInfo);
            }
        }
        return dataMap;
    }

    public static Map<Integer,ActivityInfo> getOpenActivityInfoMap(int activityType) {
        Map<Integer,ActivityInfo> dataMap = new ConcurrentHashMap<>();
        for(ActivityInfo activityInfo : activityInfoMap.values()) {
            if (activityInShowTime(activityInfo)) {
                if(activityInfo.getType() == activityType){
                    dataMap.put(activityInfo.getActivityId(),activityInfo);
                }
            }
        }
        return dataMap;
    }

    public static boolean activityInShowTime(ActivityInfo activityInfo){
        if(null == activityInfo){
            return false;
        }
        long nowTime = System.currentTimeMillis() / 1000;
        return nowTime >= activityInfo.getBeginShowTime() && nowTime < activityInfo.getEndShowTime();
    }

    public static boolean isActivityContain(ActivityInfo activityInfo1,ActivityInfo activityInfo2){
        if(null == activityInfo1 || null == activityInfo2){
            return false;
        }
        long nowTime = System.currentTimeMillis() / 1000;
        return activityInfo2.getEndShowTime() > activityInfo1.getBeginShowTime() && activityInfo2.getBeginShowTime() < activityInfo1.getEndShowTime();
    }

    public static boolean activityInTime(ActivityInfo activityInfo) {
        if(null == activityInfo){
            return false;
        }
        long now = System.currentTimeMillis() / 1000;
        return activityInfo.getBeginTime() <= now && activityInfo.getEndTime() > now;
    }

    public static ActivityInfo getInTimeActivityInfo(int activityId){
        if(!activityInfoMap.containsKey(activityId)){
            return null;
        }
        ActivityInfo activityInfo = activityInfoMap.get(activityId);
        if(activityInTime(activityInfo) && activityInfo.getActivityId() == activityId){
            return activityInfo;
        }
        return null;
    }

    public static List<ActivityConditionInfo> getActivityConditionInfoList(int activityId){
        List<ActivityConditionInfo> activityConditionInfoList = activityConditionInfoMap.get(activityId);
        if(null != activityConditionInfoList){
            return new ArrayList<>(activityConditionInfoList);
        }
        return new ArrayList<>();
    }

    public static ActivityConditionInfo getActivityConditionInfoByRank(int activityId, int rank) {
        List<ActivityConditionInfo> dataList = getActivityConditionInfoList(activityId);
        if(null != dataList) {
            for (ActivityConditionInfo conditionInfo : dataList) {
                if (rank >= conditionInfo.getParamList().get(0).intValue() && rank <= conditionInfo.getParamList().get(1).intValue()) {
                    return conditionInfo;
                }
            }
        }
        return null;
    }

    public static ActivityConditionInfo getActivityConditionInfo(int activityId,int conditionId) {
        List<ActivityConditionInfo> activityConditionInfoList = getActivityConditionInfoList(activityId);
        for(ActivityConditionInfo activityConditionInfo : activityConditionInfoList){
            if(activityConditionInfo.getConditionId() == conditionId){
                return activityConditionInfo;
            }
        }
        return null;
    }

    public static int getMaxRankByType(int activityId, int type) {
        int maxRank = 0;
        List<ActivityConditionInfo> dataList = getActivityConditionInfoList(activityId);
        if(null != dataList) {
            for (ActivityConditionInfo conditionInfo : dataList) {
                if (conditionInfo.getParamList().get(1).intValue() > maxRank) {
                    maxRank = conditionInfo.getParamList().get(1).intValue();
                }
            }
        }
        return maxRank;
    }


    public static Map<Integer, List<ActivityConditionInfo>> getActivityConditionInfoMap(int activityId) {
        ActivityInfo inTimeActivityInfo = getOpenActivityInfo(activityId);
        if(null != inTimeActivityInfo) {
            return activityConditionInfoMap;
        }
        return null;
    }

    public static synchronized void loadChefDuelPhase1(int activityId){
        reloadActivityInfo(activityId);
    }
}
